﻿using STHPMS1.DAL.D_Core;
using System.Data;
using ServiceStack.OrmLite;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore.I_DAL.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace STHPMS1.DAL.D_Repository
{
    public class B_RoadwayRepository : DalBase, IB_RoadwayRepository
    {
        public int GetAdmdiv(string rdwayid)
        {
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Scalar<int>("select ADMDIV from B_ROADWAY where RDWAYID='" + rdwayid + "'");
        }

        public double GetLength(string rdwayid)
        {
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Scalar<double>("select ENDMP-BEGINMP from B_ROADWAY where RDWAYID='" + rdwayid + "'");
        }


        public int GetDir(string rdid)
        {
            using(SqlConnection con = new SqlConnection(Adapter.ConnectionString))
            {
                string sql = "select COUNT(*) from B_INVNTORY where RDWAYID='"+rdid+"' group by DIRECTION_DICTID";
                SqlDataAdapter da = new SqlDataAdapter(sql,con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count == 1)
                    return 1;
                return 2;
            }
        }

        public IEnumerable<T> SearchRoad<T>()
            where T : class, IB_Roadway
        {
            string sql = @"select * from B_ROADWAY where RDWAYID in(
select RDWAYID from E_ROADWAYDETAIL group by RDWAYID)";
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Select<T>(sql);
        }



        public bool CheckRoadDir(string rdwayid, int dir)
        {
            IDbConnection conn = Adapter.OpenDbConnection();
            bool temp = conn.Scalar<bool>("select DIRE_ORNOT from B_ROADWAY where RDWAYID='" + rdwayid + "'");
            if(temp) //未分上下行
            {
                if(dir==(int)Common.C_Enum.DirectionEnum.All13)
                {
                    return true;
                }
                return false;
            }
            else
            {
                if (dir != (int)Common.C_Enum.DirectionEnum.All13)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 返回路线对象
        /// </summary>
        /// <param name="rdid"></param>
        /// <returns></returns>
        public IB_Roadway GetRoadObj(string rdid)
        {
            DataTable table = new DataTable();
            using (SqlConnection sqlCon = new SqlConnection(Adapter.ConnectionString))
            {
                string sql = string.Format("select * from B_ROADWAY where RDWAYID='{0}'",rdid);

                using (SqlDataAdapter da = new SqlDataAdapter(sql, sqlCon))
                {
                    da.Fill(table);
                    da.FillSchema(table, SchemaType.Source);
                }
            }
            if(table.Rows.Count>0)
            {
                B_Roadway road = new B_Roadway()
                {
                    Rdwayid = table.Rows[0]["RDWAYID"].ToString(),
                    Beginmp = double.Parse(table.Rows[0]["BEGINMP"].ToString()),
                    Endmp = double.Parse(table.Rows[0]["ENDMP"].ToString())
                };
                return road;
            }
            return null;
        }
    }
}
